package com.sinosoft.dsp.platform.components.portal.output;

import java.util.ArrayList;
import java.util.List;

import com.sinosoft.dsp.platform.components.portal.iteration.ISinoTreeDataObject;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoAbstractLazyTreeViewBean;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoTreeDataObject;
import com.sinosoft.dsp.platform.services.CodeSetService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.to.IGenericTO;
import com.sinosoft.dsp.system.to.attribute.Attribute;
import com.sinosoft.dsp.system.to.helper.ToHelper;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 人员部门选择树的ViewBean
 *   
 * 
 */
public class SinoUserOrgSelectTreeViewBean extends
		SinoAbstractLazyTreeViewBean<SinoTreeDataObject> implements
		ISinoUserOrgSelectTreeViewBean {

	public static final String INIT_PARAMETER_USER_ORG_ID = "userOrgId";

	public static final String INIT_PARAMETER_VALUE = "value";

	public static final String INIT_PARAMETER_FORM_ID = "formId";

	public static final String INIT_PARAMETER_ELEMENT_ID = "elementId";

	private String userOrgId;

	private String value;

	private String formId;

	private String elementId;
	 
	private List<SinoTreeDataObject> nodeAll = new ArrayList<SinoTreeDataObject>();
	@Override
	public void init(IGenericTO tos) throws SinoException {
		Attribute<?> userOrgIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_USER_ORG_ID);
		Attribute<?> valueA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_VALUE);
		Attribute<?> formIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_FORM_ID);
		Attribute<?> elementIdA = ToHelper.getContentAttriubteByName(tos,
				INIT_PARAMETER_ELEMENT_ID);

		if (userOrgIdA != null)
			this.userOrgId = userOrgIdA.convertToString();

		if (valueA != null)
			this.value = valueA.convertToString();

		if (formIdA != null)
			this.formId = formIdA.convertToString();

		if (elementIdA != null)
			this.elementId = elementIdA.convertToString();

	}

	@Override
	public List<SinoTreeDataObject> loadFirstLevelNodes() {
		if (!Const.isEmpty(userOrgId)) {
			CodeSetService css = SinoManagedBean.findBean("codeSetService");
			return css.getRootSubCodesTree(userOrgId);
		}
		return null;
	}

	@Override
	public List<SinoTreeDataObject> loadSubNodes(String id) {
		if (!Const.isEmpty(userOrgId) && !Const.isEmpty(id)) {
			CodeSetService css = SinoManagedBean.findBean("codeSetService");
//			nodeAll.clear();
			List<SinoTreeDataObject> nodes = css.getSubCodesTree(userOrgId, id);
			List<SinoTreeDataObject> noded = getOrgOrUser(id); 
			for(int i=0;i<nodes.size();i++){
				nodeAll.add(nodes.get(i));
			}
			
			for(int j=0;j<noded.size();j++){
				nodeAll.add(noded.get(j));
			}
			
			return nodeAll;
			
		}
		return null;
	}

	public String getUserOrgId() {
		return userOrgId;
	}

	public void setUserOrgId(String userOrgId) {
		this.userOrgId = userOrgId;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getElementId() {
		return elementId;
	}

	public void setElementId(String elementId) {
		this.elementId = elementId;
	}

	public String getSelectedKey() {
		if (this.getSelectedNode() != null) {
			SinoTreeDataObject tdo = (SinoTreeDataObject) this
					.getSelectedNode().getData();
			return tdo.getId();
		}
		return "";
	}

	public String getSelectedName() {
		if (this.getSelectedNode() != null) {
			SinoTreeDataObject tdo = (SinoTreeDataObject) this
					.getSelectedNode().getData();
			return tdo.getName();
		}
		return "";
	}
	
	public List<SinoTreeDataObject> getOrgOrUser(String pId){
		/*
		IUserService userService = SinoManagedBean.findBean("userService");
		List<SinoTreeDataObject> users = new ArrayList<SinoTreeDataObject>();
		users.clear(); 
		if(this.getCurrentNode() != null){   
			List<Object[]> orgUsers = userService.findOrgAndUserByOrgUUID(pId);  
			for (int i = 0; i < orgUsers.size(); i++) {
				Object[] objs = (Object[]) orgUsers.get(i);
				SinoTreeDataObject nodes = new SinoTreeDataObject(); 
				nodes.setId(objs[0].toString());
				nodes.setValue(objs[1].toString());
				nodes.setName(objs[2].toString());
				nodes.setType(ISinoTreeDataObject.TREE_TYPE_LEAF);
				nodes.setParentId(pId);
				users.add(nodes);
			}
		} 
		
		return users; 
		*/
		return null;
	}

}
